import { request } from 'umi';

import APIS from '@/services/apis';
import type { HttpResponseWrap } from '@/types/Common';
import type { ISystemNormal } from '@/types/System';
import type { CurrentUserDto, ILoginResponse } from '@/types/User';

// 用户登录
export async function reFreshToken(params: any) {
  return request(APIS.Refresh, {
    method: 'POST',
    data: params,
  });
}

// // 验证码登录
// export async function sendPhoneLoginSMSCode(params: any) {
//   return request(APIS.SendPhoneLoginSMSCode, {
//     method: 'POST',
//     data: params,
//   });
// }

// 用户登录
export async function loginUser(params: any) {
  return request(APIS.Login, {
    method: 'POST',
    data: params,
  });
}
// 忘记密码发送验证码
export async function forgetPsd(params: any) {
  return request(APIS.SendPhoneForgetPaswordSMSCode, {
    method: 'POST',
    data: params,
  });
}
// 忘记密码
export async function resetPasswordByPhoneNo(params: any) {
  return request(APIS.ResetPasswordByPhoneNo, {
    method: 'POST',
    data: params,
  });
}
// 获取用户信息
export async function userInfo() {
  return request(`/api/SaasUser/Info`, {
    method: 'GET',
  });
}
// 修改用户信息
export async function editUserInfo(params: any) {
  return request(APIS.EditUserInfo, {
    method: 'POST',
    data: params,
  });
}

// 修改用户密码
export async function editUserPassword(params: any) {
  return request(APIS.EditUserPassword, {
    method: 'POST',
    data: params,
  });
}

// 注册用户
export async function registerUser(params: any) {
  return request(APIS.Register, {
    method: 'POST',
    data: params,
  });
}
// 注册用户成功回调
export async function registerSuccessCb(params: any) {
  return request(`/api/SaasUser/ConfirmEmail?userId=${params.userId}&code=${params.code}`, {
    method: 'GET',
  });
}
// 获取手机验证码
export async function sendSms(params: any) {
  return request(APIS.GetSmsCode, {
    method: 'POST',
    data: params,
  });
}
/**
 * 获取验证码图片
 */
export async function getCheckCodeImg(guid: string) {
  return request('/api/User/CheckCodeImageCreate', {
    method: 'POST',
    data: { guid },
    responseType: 'blob',
  });
}

export async function checkCompanyInfo(params: API.CheckCommunityParamsType) {
  return request('/api/Company/CheckCompanyInfo', {
    method: 'POST',
    data: params,
  });
}

/**
 * 登录校验用户名和密码
 * @param params 登录参数
 */
export async function companyLogin(params: API.CheckPasswordParamsType) {
  return request<HttpResponseWrap<ILoginResponse>>('/api/User/CompanyLogin', {
    method: 'POST',
    data: params,
  });
}

/**
 * 获取当前登录用户信息
 */
export async function my() {
  return request<HttpResponseWrap<{ userInfo: CurrentUserDto }>>('/api/User/my');
}

/**
 * 获取可用系统
 */
export async function getSystems() {
  return request<HttpResponseWrap<{ data: ISystemNormal[] }>>('/api/System/Systems', {
    method: 'POST',
  });
}

/**
 * 获取二维码
 */
export async function getQrCode() {
  return request('/api/Company/GetImageId');
}

/**
 * 轮询获取token
 */
export async function getToken(id: string) {
  return request(`/api/Company/GetToken/${id}`);
}

/**
 * 修改密码
 * @param id 用户ID
 * @param oldPassword 旧密码
 * @param newPassword 新密码
 */
export async function changepassword(id: string, oldPassword: string, newPassword: string) {
  return request(`/api/User/changepassword`, {
    method: 'POST',
    data: {
      id,
      oldPassword,
      newPassword,
    },
  });
}

/**
 * 获取用户脱敏信息
 * @param companyId 社区号
 * @param loginAccount 登录用户名
 */
export async function getUserDesensitizationInfo(companyId: string, loginAccount: string) {
  return request('/api/User/UserInfo', {
    method: 'POST',
    data: {
      companyId,
      loginAccount,
    },
  });
}

/**
 * 校验短信验证码
 * @param smsId 短信ID
 * @param userId 用户ID
 * @param verifyCode 短信验证码
 */
export async function getVerifySMSCodeResult(smsId: string, userId: string, verifyCode: string) {
  return request('/api/User/VerifySMSCode', {
    method: 'POST',
    data: {
      smsId,
      userId,
      verifyCode,
    },
  });
}

/**
 * 重置密码
 * @param smsId 短信ID
 * @param userId 用户ID
 * @param verifyCode 短信验证码
 * @param newPassword 新密码
 */
export async function ResetPassword(
  smsId: string,
  userId: string,
  verifyCode: string,
  newPassword: string,
) {
  return request('/api/User/ResetPassword', {
    method: 'POST',
    data: {
      smsId,
      userId,
      verifyCode,
      newPassword,
    },
  });
}
